package com.rico.deploy.controller;

import com.github.pagehelper.PageInfo;
import com.rico.common.response.MessageUtils;
import com.rico.common.response.RestResponse;
import com.rico.deploy.entity.*;
import com.rico.deploy.service.*;
import com.rico.deploy.vo.ClassroomVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author 夏智力
 * @FileName SubjectController
 * @date 2021/5/7 11:27
 * @Software: IntelliJ IDEA
 */
@Controller
@Slf4j
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private SubjectTypeService subjectTypeService;

    @Autowired
    private ClassroomTypeService classroomTypeService;

    /**
     * 默认获取第一页学科列表
     * @return
     */
    @GetMapping("subject/list")
    @ResponseBody
    public RestResponse getlist(Integer page, Integer limit) {
        log.info("{} {}", page, limit );
        List<Subject> subjects = subjectService.selectAll(page, limit);
        PageInfo<Subject> subjectPageInfo = new PageInfo(subjects);
        return MessageUtils.success(subjectPageInfo.getList(), (int) subjectPageInfo.getTotal());
    }

    @GetMapping("subject/lists")
    @ResponseBody
    public RestResponse getlist(Integer page, Integer limit,@RequestParam(value="keyword",defaultValue = "计算机科学与技术") String keyword) {
        log.info("{} {} {}", page, limit, keyword );
        List<Subject> subjects = subjectService.selectAll(page, limit ,keyword);
        PageInfo<Subject> subjectPageInfo = new PageInfo(subjects);
        return MessageUtils.success(subjectPageInfo.getList(), (int) subjectPageInfo.getTotal());
    }


    @GetMapping("subject/delete")
    @ResponseBody
    public RestResponse deleteById(Integer id) {
        log.info("{}", id);
        String msg = null;
        if ( subjectService.deleteSubjectById(id) == 1 )
            msg = "删除成功";
        else
            msg = "删除失败";
        return MessageUtils.success(msg);
    }

    /**
     * 更新数据
     * @param subject
     * @return
     */
    @PostMapping("subject/put")
    @ResponseBody
    public RestResponse put(Subject subject) {

        subject.setClassroomType(classroomTypeService.selectById(subject.getClassroomTypeId()).getTypeName());

        subject.setSubjectType(subjectTypeService.selectById(subject.getSubjectTypeId()).getSubjectType());

        subject.setModifyTime( new Date(new Date().getTime()+28800000));

        int i = subjectService.updateSubject(subject);
        if ( i == 0 )
            return MessageUtils.fail("编辑失败");

        else
            return MessageUtils.success("编辑成功");
    }

    /**
     * 添加数据
     * @param subject
     * @return
     */

    @PostMapping("subject/post")
    @ResponseBody
    public RestResponse post(Subject subject) {

        subject.setClassroomType(classroomTypeService.selectById(subject.getClassroomTypeId()).getTypeName());

        subject.setSubjectType(subjectTypeService.selectById(subject.getSubjectTypeId()).getSubjectType());

        subject.setModifyTime( new Date(new Date().getTime()+28800000));

        subject.setReleaseTime( new Date(new Date().getTime()+28800000));

        int i = subjectService.insertSubject(subject);
        if ( i == 0 )
            return MessageUtils.fail("添加失败");

        else
            return MessageUtils.success("添加成功");
    }

    /**
     * 添加页面
     * @param
     * @return
     */
    @GetMapping("subject/add")
    public String htmladd(Model model){
        List<SubjectType> subjectTypes = subjectTypeService.selectAll();
        List<ClassroomType> classroomTypes = classroomTypeService.selectAll();

        model.addAttribute("subjectTypes",subjectTypes);
        model.addAttribute("classroomTypes",classroomTypes);
        return "page/manage/subjectManage/add";
    }

    /**
     * 学院左边树
     * @return
     */
    @GetMapping("subject/leftMenu")
    @ResponseBody
    public RestResponse LeftTreeView(){
        return MessageUtils.success("操作成功",subjectService.LeftTreeView());
    }

    @GetMapping("subject/edit")
    public String htmledit(Integer id, Model model){
        log.info(id.toString());
        Subject subject = subjectService.selectById(id);
        List<SubjectType> subjectTypes = subjectTypeService.selectAll();
        List<ClassroomType> classroomTypes = classroomTypeService.selectAll();

        model.addAttribute("subject",subject);
        model.addAttribute("subjectTypes",subjectTypes);
        model.addAttribute("classroomTypes",classroomTypes);
        return "page/manage/subjectManage/edit";
    }
    /**
     * 获取html页面
     * @return
     */
    @GetMapping("subjectManage")
    public String gethtml(){
        return "page/manage/subjectManage/subjectManage";
    }

}
